﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using DLL;

public partial class ExamDesign : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //绑定数据源
        if (!IsPostBack)
        {
            BindData();
            //HideElement(); 
        }
        viewIsNoData();
    }

    //绑定数据源
    private void BindData()
    {
        //绑定试卷数据源
        gvExam.DataSource = examDesign_BLL.GetSource();
        gvExam.DataBind();

        //绑定科目数据源
        dropKM.DataSource = km_BLL.GetSource();
        dropKM.DataTextField = "kmName";
        dropKM.DataValueField = "kmNum";
        dropKM.DataBind();
    }

    //提交
    protected void btnRefur_Click(object sender, EventArgs e)
    {
        if (txtName.Text == "" || txtTotalScore.Text == "" || txtEligibleScore.Text == "" || txtVer.Text == "")
        {
            ShowMessage("数据信息不完整！");
            return;
        }
        //创建数据对象
        ExamPaper newPaper = new ExamPaper();
        //为数据对象赋值 
        newPaper.examName = txtName.Text;
        newPaper.kmNum = decimal.Parse(dropKM.SelectedValue);
        newPaper.examVer = DateTime.Parse(txtVer.Text);
        newPaper.bFB = chkZF.Checked;
        newPaper.examType = dropCETGrade.SelectedValue;
        newPaper.eligibleScore = float.Parse(txtEligibleScore.Text);
        newPaper.totalScore = float.Parse(txtTotalScore.Text);
        newPaper.timeOver = int.Parse(txtTimeOver.Text == "" ? "0" : txtTimeOver.Text);

        if (btnRefur.Text == "添加")
        {
            bool bl = examDesign_BLL.Add(newPaper);
            if (bl)
            {
                Response.Redirect("ExamDesign.aspx?examType=en");
            }
            else
                ShowMessage("添加失败！");
        }
        else
        {
            bool bl = examDesign_BLL.Update(seleExamNum, newPaper);
            if (bl)
            {
                Response.Redirect("ExamDesign.aspx?examType=en");
            }
            else
                ShowMessage("更新失败！");
        }
    }
    //选择行
    private static string seleExamNum = "";
    protected void gvExam_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        //获取当前选中试卷编号
        seleExamNum = gvExam.DataKeys[e.NewSelectedIndex][0].ToString();
        //获取当前选中行
        GridViewRow row = gvExam.Rows[e.NewSelectedIndex];
        //清除科目下拉列表中的历史选中项
        dropKM.ClearSelection();
        //清除英语等级下拉列表中的历史选中项
        dropCETGrade.ClearSelection();
        //指定科目的当前项
        dropKM.Items.FindByText(row.Cells[0].Text).Selected = true;
        //填充试卷名称文本框
        txtName.Text = ((HyperLink)row.Cells[2].Controls[0]).Text;
        //填充总分文本框
        txtTotalScore.Text = row.Cells[3].Text;
        //填充合格分文本框
        txtEligibleScore.Text = row.Cells[4].Text;
        //填充试卷版本文本框
        txtVer.Text = row.Cells[6].Text;
        //指定考试类别当前项
        dropCETGrade.Items.FindByText(row.Cells[7].Text).Selected = true;
        //获取考试时间限制
        txtTimeOver.Text = row.Cells[5].Text;
        //作废
        chkZF.Checked = row.Cells[8].Text == "是" ? true : false;
        //更改提交按钮状态
        btnRefur.Text = "更新";
    }
    //取消
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        //清除科目下拉列表中的历史选中项
        dropKM.ClearSelection();
        //清除英语等级下拉列表中的历史选中项
        dropCETGrade.ClearSelection();
        txtName.Text = txtTotalScore.Text = txtEligibleScore.Text = txtVer.Text = "";
        chkZF.Checked = false;
        //更改提交按钮状态
        btnRefur.Text = "添加";
    }
    //翻页
    protected void gvExam_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvExam.PageIndex = e.NewPageIndex;
        BindData();
    }
    //删除
    protected void gvExam_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        bool bl = examDesign_BLL.Delete(gvExam.DataKeys[e.RowIndex][0].ToString());
        if (bl)
        {
            ShowMessage("删除成功！");
            BindData();
        }
        else
            ShowMessage("删除失败！");
    }
    protected void btnTXMana_Click(object sender, EventArgs e)
    {
        if (seleExamNum == "")
        {
            ShowMessage("请选择一套试卷！");
            return;
        }
        else
            Response.Redirect("txDesign.aspx?examNum=" + seleExamNum + "&examName=" + txtName.Text);
    }

    #region 脚本
    /// <summary>
    /// 如果GridView中没有数据则显示提示信息
    /// </summary>
    protected void viewIsNoData()
    {
        if (gvExam.Rows.Count == 0)
        {
            RegisterStartupScript("prompt1", "<script type='text/javascript'>document.getElementById('divBrowse').innerHTML='(尚未添加相关数据)'</script>");
        }
    }
    /// <summary>
    /// 显示消息
    /// </summary>
    /// <param name="msg">消息内容</param>
    protected void ShowMessage(string msg)
    {
        RegisterStartupScript("msg", "<script type='text/javascript' language='javascript'>alert('" + msg + "')</script>");
    }

    //隐藏节点
    private void HideElement()
    {
        string examType = "";
        if (examType == "qt")
        {
            RegisterStartupScript("prompt2", "<script type='text/javascript'>fnHideNode()</script>");
        }
    }
    #endregion
}
